<?php namespace Core\Myapp\Database;

use Core\Myapp\Zobero;

class Model extends Zobero
{
    protected $_isLoaded = false;

    protected $_table;

    protected $_primaryId = 'id';

    protected $_id = null;

    protected $_data = array();

    protected $_select = array('*');

    protected $_join = array();

    protected $_limit;

    protected $_conditions = array();

    protected $_queryType = '';

    public function __get($name)
    {
        return $this->_data[$name];
    }

    public function __set($name, $value)
    {
        $this->_data[$name] = $value;
        return $this;
    }
    public function getId()
    {
        return $this->_id;
    }
    public function getData()
    {
        return $this->_data;
    }

    public function createInstance()
    {
        return new self;
    }

    public function getPrimaryId()
    {
        return $this->_primaryId;
    }

    public function setData()
    {

    }

    /**
     * @param $table table name to join
     * @param $type JOIN, LEFT JOIN, RIGHT JOIN ....
     * @param $on string (mt.id = jt.id AND mt.name = jt.name) (mt : Main table, jt : Joint table)
     * @return $this
     */

    public function addJoin($table, $type, $on)
    {
        $this->_join[] = array(
            'table'=>$table,
            'type'=>$type,
            'on'=> $on
        );
        return $this;
    }

    protected function unsetJoin()
    {
        $this->_join = array();
        return $this;
    }

    protected function unsetConditions()
    {
        $this->_conditions = array();
        return $this;
    }

    public function load($id)
    {
        if($this->_isLoaded === true)
            return $this;
        $this->unsetJoin();
        $this->unsetConditions();
        $this->_id = $id;
        $this->_queryType = 'SELECT';
        $this->_select = array('*');
        $this->_conditions = array("{$this->_primaryId} = :id");
        $this->query();
    }

    /**
     * @param $query SELECT, UPDATE, INSERT, DELETE
     */
    public function setQueryType($query)
    {
        $this->_queryType = $query;
        return $this;
    }

    public function getQueryType()
    {
        return $this->_queryType;
    }

    public function save()
    {
        $this->unsetJoin();
        $this->unsetConditions();
        if($this->_isLoaded === true)
        {
            $this->setQueryType('UPDATE');
            $this->_conditions = array("{$this->_primaryId} = :id");
        }
        else
        {
            $this->setQueryType('INSERT');
            $this->_conditions = array();
        }
        $processor = $this->getSingleton('Core\Myapp\Database\Processor');
        $this->_isLoaded = true;
        $processor->save($this);
    }

    public function query()
    {
        if($this->_isLoaded === true)
            return $this;
        $processor = $this->getSingleton('Core\Myapp\Database\Processor');
        $this->_isLoaded = true;
        $processor->run($this);
    }

    public function getTable()
    {
        return $this->_table;
    }

    public function getSelect()
    {
        return $this->_select;
    }

    public function setColumnsToSelect()
    {

    }

    public function getJoins()
    {
        return $this->_join;
    }
    public function getConditions()
    {
        return $this->_conditions;
    }
    public function addCondition($condition)
    {
        $this->_conditions[] = $condition;
        return $this;
    }
}